package com.gxuwz.leave.dao;

import com.gxuwz.leave.entity.Instructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class InstDaoImpl implements InstDao {

    private JdbcTemplate jdbcTemplate;

    @Override
    public void insert(Instructor entity) {
        String sql = "insert into sys_instructor(instID, instName, password, depID, telephone) values (?, ?, ?, ?, ?)";
        List<Object> paramList = new ArrayList<>();
        if(entity==null){
            return;
        }else{
            paramList.add(entity.getInstID());
            paramList.add(entity.getInstName());
            paramList.add(entity.getPassword());
            paramList.add(entity.getDepID());
            paramList.add(entity.getTelephone());
        }
        jdbcTemplate.update(sql, paramList.toArray());
    }

    @Override
    public void delete(String insID) {
        String sql = "delete from sys_instructor where instID = ?";
        if(insID==null||"".equals(insID)){
            return;
        }else{
            jdbcTemplate.update(sql, insID);
        }
    }

    @Override
    public void update(Instructor entity) {
        String sql = "update sys_instructor set ";
        List<Object> paramList = new ArrayList<>();
        if(entity==null){
            return;
        }else{
            if(entity.getInstName()!=null && !"".equals(entity.getInstName())){
                sql += "instName = ?, ";
                paramList.add(entity.getInstName());
            }
            if(entity.getPassword()!=null && !"".equals(entity.getPassword())){
                sql += "password = ?, ";
                paramList.add(entity.getPassword());
            }
            if(entity.getDepID()!=null && !"".equals(entity.getDepID())){
                sql += "depID = ?, ";
                paramList.add(entity.getDepID());
            }
            if(entity.getTelephone()!=null && !"".equals(entity.getTelephone())){
                sql += "telephone = ?, ";
                paramList.add(entity.getTelephone());
            }

            // 移除多余的逗号和空格
            sql = sql.substring(0, sql.length() - 2);
            sql+="where instID = ?";
            paramList.add(entity.getInstID());
        }
        jdbcTemplate.update(sql, paramList.toArray());
    }

    @Override
    public List<Instructor> query(Instructor entity) {
        String sql = "select * from sys_instructor where 1=1 ";
        List<Instructor> InstList = new ArrayList<>();
        List<Object> paramList = new ArrayList<>();
        if(entity!=null){
            if(entity.getInstID()!=null && !"".equals(entity.getInstID())){
                sql+="and instID = ?";
                paramList.add(entity.getInstID());
            }
            if(entity.getPassword()!=null && !"".equals(entity.getPassword())){
                sql+="and password = ?";
                paramList.add(entity.getPassword());
            }
            if(entity.getInstName()!=null && !"".equals(entity.getInstName())){
                sql+="and instName like ?";
                paramList.add("%" + entity.getInstName() + "%");
            }
        }
        InstList = jdbcTemplate.query(sql, new RowMapper<Instructor>() {
            @Override
            public Instructor mapRow(ResultSet resultSet, int i) throws SQLException {
                Instructor inst= new Instructor();
                inst.setInstID(resultSet.getString("instID"));
                inst.setInstName(resultSet.getString("instName"));
                inst.setPassword(resultSet.getString("password"));
                inst.setDepID(resultSet.getString("depID"));
                inst.setTelephone(resultSet.getString("telephone"));
                return inst;
            }
        }, paramList.toArray());
        return InstList;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate = jdbcTemplate;
    }
}
